﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using QXBook.Service;
using QXBook.Model;
using QXBook.Utils;
using Web.App_Code;
using QXBook.Controls.HtmlControls;

namespace Web.Article
{
    public partial class Article_List : UserBasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                ArticleCondition condition = new ArticleCondition();

                if (!string.IsNullOrEmpty(CommonUtils.Get("Part")))
                {
                    condition.PartType = (Part)CommonUtils.ToInt(CommonUtils.Get("Part"));
                }

                if (!string.IsNullOrEmpty(CommonUtils.Get("CategoryID")))
                {
                    condition.CategoryID = CommonUtils.ToInt(CommonUtils.Get("CategoryID"));
                }

                this.TextBind(condition);
            }
        }

        private void TextBind(ArticleCondition condition)
        {
            PagerState ps = new PagerState();
            if (string.IsNullOrEmpty(CommonUtils.Get("page")))
            {
                ps.CurrentPage = 1;
            }
            else
            {
                ps.CurrentPage = CommonUtils.ToInt(CommonUtils.Get("page"));
            }
            ps.PageSize = 20;

            List<ArticleInfo> list = ServiceFactory.CreateInstance<ArticleService>().GetModelList(ref ps, condition);

            this.rptList.DataSource = list;
            this.rptList.DataBind();

            divPageTop.Text = ClientPager.CreatePager(ps);
            divPageBottom.Text = ClientPager.CreatePager(ps);
        }

        protected void rptList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            ArticleInfo article = (ArticleInfo)e.Item.DataItem;
            Label lblAddDate = (Label)e.Item.FindControl("lblAddDate");
            Anchor aTitle = (Anchor)e.Item.FindControl("aTitle");
            PlaceHolder phImg = (PlaceHolder)e.Item.FindControl("phImg");

            lblAddDate.Text = article.AddDate.ToString("yyyy-MM-dd");

            aTitle.Text = article.Title;
            aTitle.Href = "'Article_View.aspx?ArticleID=" + article.ArticleID.ToString();

            if (!string.IsNullOrEmpty(article.SmallImg))
            {
                phImg.Visible = true;
            }
            else
            {
                phImg.Visible = false;
            }
        }
    }
}